// CadToolPolyLine.cpp
//
#include "CadToolPolyLine.h"

#include "CadEngine.h"

#include "OpenGL/Box.h"
#include "OpenGL/OpenGL.h"
#include "OpenGL/Renderer.h"

//-------------------------------------------------------------------------------------------------
CadToolPolyLine::CadToolPolyLine()
: CadTool("PolyLine",2)
{
	_chained       = true;
	_creationColor = OpenGL::Color::red().lighter();
}

//-------------------------------------------------------------------------------------------------
CadToolPolyLine::~CadToolPolyLine()
{}

//-------------------------------------------------------------------------------------------------
void CadToolPolyLine::execute()
{
	if( isChained() )
		chainPoint();
	else
	{
		if( _nPoints > 1 )
		{
			CadEngine::Command c;

			c.command   = CadEngine::Create;
			c.primitive = CadEngine::PolyLine;
			c.color     = _creationColor;
			for( size_t i=0; i<_nPoints-1; ++i )
				c.points.push_back( _points[i] );
			c.attributes += _objectAttributes;

			_manager->issueCommand(c);
		}
	}
}

// --------------------------------------------------------------------------------------------
void CadToolPolyLine::reset()
{
	_totPoints = 2;

	CadTool::reset();

	_chained = true;
}

// --------------------------------------------------------------------------------------------
void CadToolPolyLine::setCreationColor( const OpenGL::Color &c ) { _creationColor = c; }

// --------------------------------------------------------------------------------------------
OpenGL::Color CadToolPolyLine::creationColor() const { return _creationColor; }

//-------------------------------------------------------------------------------------------------
void CadToolPolyLine::draw( OpenGL::Renderer *r )
{
	const OpenGL::Math::dvec3 o = r->origin();

	glPushAttrib( GL_LINE_BIT|GL_ENABLE_BIT|GL_POINT_BIT );
	glEnable(GL_LINE_STIPPLE);
	glLineStipple(1, 0x00FF);
	glLineWidth(2);	
	glColor4fv( _manager->settings().previewColor().rgba() );

	if( _nPoints > 1 )
	{
		glBegin(GL_LINE_STRIP);
			for( size_t i=0; i<_nPoints; ++i )
				glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
		glEnd();
	}

	glDisable( GL_LINE_STIPPLE );

	glPointSize(7.0);
	glColor3ub(0x00, 0x00, 0x00);
	glBegin(GL_POINTS);
		for( size_t i=0; i<_nPoints; ++i )
			if( i != _selIndex ) glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
	glEnd();

	glPointSize(5.0);
	glColor4fv( _manager->settings().previewColor().rgba() );
	glBegin(GL_POINTS);
		for( size_t i=0; i<_nPoints; ++i )
			if( i != _selIndex ) glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
	glEnd();

	//glPointSize(5.0);
	//glBegin(GL_POINTS);
	//	for( size_t i=0; i<_nPoints; ++i )
	//		glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
	//glEnd();


	if( _selIndex >= 0 )
	{
		glPointSize(9.0);
		glColor3ub(0x00, 0x00, 0x00);
		glBegin(GL_POINTS);
			glVertex2dv( OpenGL::Math::value_ptr(_points[_selIndex]-o) );
		glEnd();

		glPointSize(7.0);
		glColor4fv( _manager->settings().previewColor().rgba() );
		glBegin(GL_POINTS);
			glVertex2dv( OpenGL::Math::value_ptr(_points[_selIndex]-o) );
		glEnd();
	}


	glPopAttrib();
}